﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S3.Common;
using S3.DataModel;

namespace S3.Biz
{
    public class ExpenseIncomesManager : CommonManager
    {
        #region Delete

        /// <summary>
        /// Xoa 1 ban ghi
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public int Delete(string key)
        {
            try
            {
                //Check deplicate
                var user = _entities.ExpenseIncomes.FirstOrDefault(a => a.DocumentNo.ToLower() == key.ToLower());

                //If found
                if (user != null)
                {
                    _entities.DeleteObject(user);                    
                }

               return _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("ExpenseIncomesManager::Delete::", ex, true);
                }
            }

            return 0;
        }

        /// <summary>
        /// Xoa tat ca ban ghi
        /// </summary>
        /// <param name="deskName"></param>
        /// <returns></returns>        
        public IEnumerable<ExpenseIncome> DeleteAll()
        {
            try
            {
                var list = _entities.ExpenseIncomes.ToList();

                if (list != null)
                {
                    list.ForEach(_entities.DeleteObject);

                    _entities.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("ExpenseIncomes::DeleteAll::", ex, true);
                }
            }

            return null;
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Load tat ca ban ghi
        /// </summary>
        /// <param name="deskName"></param>
        /// <returns></returns>
        
        public IQueryable<ExpenseIncome> GetAll()
        {
            try
            {
                return _entities.ExpenseIncomes.OrderByDescending(i => i.ModifiedDate);

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("ExpenseIncomes::GetAll::", ex, true);
                }
            }

            return null;
        }
        #endregion

        #region Save
        /// <summary>
        /// Save entry to SQL
        /// </summary>
        /// <param name="item"></param>
        public void Save(ExpenseIncome item)
        {
            try
            {
                ExpenseIncome entry = null;
                //Check exist first
                // If existing then update else add new
                if (IsExist(ref entry))
                {
                    entry.Reason = item.Reason;

                    entry.Category = item.Category;
                    entry.Amount = item.Amount;
                    entry.ModifiedBy = item.ModifiedBy;
                    entry.ModifiedDate = DateTime.Now;
                    entry.ReferrenceDocumentNo = item.ReferrenceDocumentNo;
                    entry.Note = item.Note;
                    entry.Status = item.Status;

                }
                else
                {
                    //Add new
                    if (!string.IsNullOrEmpty(item.DocumentNo))
                    {
                        _entities.AddToExpenseIncomes(item);
                    }
                }

                //Submit change
                _entities.SaveChanges();
                
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("ExpenseIncomesManager::Save::", ex, true);
                }
            }

        }
        #endregion

        /// <summary>
        /// Check existing or not
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool IsExist(ref ExpenseIncome input)
        {
            //Check parameter
            if (input == null) return false;

            var key = input.DocumentNo;

            try
            {
                var output = _entities.ExpenseIncomes.Where(a => a.DocumentNo.ToLower() == key.ToLower());
                return output.Count() > 0;
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("ExpenseIncomeManager::IsExist::", ex, true);
                }
            }

            return false;
        }
    }
}